#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
  int pipefd[2] = {0};
  pipe(pipefd);
  //pipefi[0] write, else read 
  pid_t id = fork();
  if(id == 0)
  {
    //child
    close(pipefd[0]);
    const char* msg = "this is the data that child process wrote";
    int count = 0;
    while(1)
    {
      write(pipefd[1], msg, strlen(msg));
      printf("The number of times the child process writes:%d\n", count++);
      sleep(1);
    }
  }
  else{
    //father
    close(pipefd[1]);
    char buffer[64] = {0};
    int count = 0;
    while(1)
    {
      ssize_t ret = read(pipefd[0], buffer, sizeof(buffer) - 1);
      //judge whether to read in 
      if(ret > 0)
      {
        buffer[ret] = '\0';
        printf("the father process got the information: %s\n",buffer);
        sleep(0);
      }
      else{
        
       printf("error\n");
      }
      if(count++ == 5)
      {
        close(pipefd[0]);
      }
    }
  }

  return 0;
}
